package Hot100;

import java.util.Arrays;

public class 合并区间 {
    public int[][] merge(int[][] intervals) {
        Arrays.sort(intervals, (x, y) -> x[0] - y[0]);
        int[][] results = new int[intervals.length][2];
        int index = -1;
        for (int[] interval : intervals) {
            if (index == -1 || interval[0] > results[index][1]) results[++index] = interval;
            else results[index][1] = Math.max(results[index][1], interval[1]);
        }
        return Arrays.copyOf(results, index + 1);
    }
}
